<template>
<div class="wechat-public-menu views-container">
    <el-dialog @close="saveDialogForm" :visible.sync="dialogTableVisible" custom-class="reset-pwd wlm-form-dialog dialog-table">
        <div class="wlm-form" v-if="dialogTableVisible">
            <div class="wlm-form-header wlm-dialog-fixheader">
                <span class="wlm-dialog-inlenr">{{`${!!formFormatData.api['editForm']['params']['id']?'编辑':'添加'}菜单`}}</span>
            </div>
            <el-scrollbar wrap-class="scrollbar-wrapper">
                <div class="wlm-form-content">
                    <div class="wlm-dialog-main">
                        <div class="wlm-dialog-content">
                            <el-form ref="user" :model="formFormatData.formData" :rules="formFormatData.rules" size="small" label-width="120px" class="retail-form" label-position="right">
                                <el-form-item label="级别">
                                    <el-input v-model.trim="formFormatData.formData.name">
                                    </el-input>
                                </el-form-item>
                                <el-form-item label="名称">
                                    <el-input v-model.trim="formFormatData.formData.mobile">
                                    </el-input>
                                </el-form-item>
                               <el-form-item label="排序">
                                    <el-input v-model.trim="formFormatData.formData.mobile">
                                    </el-input>
                                </el-form-item>
                               <el-form-item label="类型">
                                    <el-input v-model.trim="formFormatData.formData.mobile">
                                    </el-input>
                                </el-form-item>
                                <el-form-item label="链接">
                                    <el-input v-model.trim="formFormatData.formData.mobile">
                                    </el-input>
                                </el-form-item>
                                <el-form-item label="小程序链接">
                                    <el-input v-model.trim="formFormatData.formData.mobile">
                                    </el-input>
                                </el-form-item>
                            </el-form>
                        </div>
                    </div>
                </div>
            </el-scrollbar>
        </div>
        <footer class="flex-row flex-align-c flex-justify-c wlm-dialog-fixfooter">
            <el-button size="small" @click="saveDialogSubmit" type="primary">保存</el-button>
        </footer>
    </el-dialog>
    <div class="wlm-table">
        <div class="wlm-table-hearder-btn">
            <el-button size="small" type="primary" @click="dialogTableVisible = true;initChooseUser.closable = true;">添加菜单</el-button>
            <el-button size="small" type="danger" >发布</el-button>
            <el-button size="small" type="danger" >删除</el-button>
        </div>
        <div class="big-warning flex-col">
                <p>1、自定义菜单最多包括3个一级菜单，每个一级菜单最多包含5个二级菜单。</p>
                <p>2、一级菜单最多4个汉字，二级菜单最多7个汉字，多出来的部分将会以“...”代替。</p>
                <p>3、创建自定义菜单后，菜单的刷新策略是，在用户进入公众号会话页或公众号profile页时，如果发现上一次拉取菜单的请求在5分钟以前，就会拉取一下菜单，如果菜单有更新，就会刷新客户端的菜单。测试时可以尝试取消关注公众账号后再次关注，则可以看到创建后的效果。</p>
        </div>
        <div class="wlm-table-content">
            <el-table :ref="tableFormatData.userTable.key" :data="tableFormatData.userTable.tableData" @selection-change="handleSelectionChange" style="width: 100%">
                <!-- <el-table-column type="selection" width="55">
                </el-table-column> -->
                <el-table-column type="expand">
                    <template slot-scope="props">
                        <el-table class="expand" :data="tableFormatData.userTable.tableData" style="width: 100%">
                            <el-table-column prop="date" label="名称" >
                                <template slot-scope="scope" v-if="scope.row.users">
                                    <img class="logo" :src="scope.row.users.avatarUrl">
                                    <span> {{scope.row.users.nickName}}</span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="date" label="关键字/链接" >
                                <template slot-scope="scope">
                                  <span>{{scope.row.name}}</span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="name" label="小程序链接" >
                                <template slot-scope="scope">
                                    <span>{{scope.row.mobile}}</span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="name" label="排序" >
                                <template slot-scope="scope">
                                    <span v-if="scope.row.type=='0'">{{scope.row.store?scope.row.store.name:''}}</span>
                                    <span v-if="scope.row.type=='1'">-</span>
                                </template>
                            </el-table-column>
                            <el-table-column label="操作" >
                                <template slot-scope="scope">
                                    <div class="operation-group">
                                        <el-button class="wlm-text" type="text" @click="initChooseUser.closable = false;formFormatDialogEditEvt({id:scope.row.id,toggle:'dialogTableVisible'})">编辑</el-button>
                                        <div class="btn-line"></div>
                                        <el-button class="wlm-text" type="text" @click="delTableItem(scope.row.id)">删除</el-button>
                                    </div>
                                </template>
                            </el-table-column>
                        </el-table>
                    </template>
                </el-table-column>
                <el-table-column prop="date" label="名称" >
                    <template slot-scope="scope" v-if="scope.row.users">
                        <img class="logo" :src="scope.row.users.avatarUrl">
                        <span> {{scope.row.users.nickName}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="date" label="关键字/链接" >
                    <template slot-scope="scope">
                      <span>{{scope.row.name}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="小程序链接" >
                    <template slot-scope="scope">
                        <span>{{scope.row.mobile}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="排序" >
                    <template slot-scope="scope">
                        <span v-if="scope.row.type=='0'">{{scope.row.store?scope.row.store.name:''}}</span>
                        <span v-if="scope.row.type=='1'">-</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" >
                    <template slot-scope="scope">
                        <div class="operation-group">
                            <el-button class="wlm-text" type="text" @click="initChooseUser.closable = false;formFormatDialogEditEvt({id:scope.row.id,toggle:'dialogTableVisible'})">编辑</el-button>
                            <div class="btn-line"></div>
                            <el-button class="wlm-text" type="text" @click="delTableItem(scope.row.id)">删除</el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-content flex-row flex-justify-e flex-align-c">
                <!-- <el-row class="pagination-btns">
                    <el-checkbox class="check-all" true-label="1" false-label="0" v-model="tableFormatData.userTable.files.checkall" @change="toggleSelection">全部</el-checkbox>
                    <el-button class="right-8" :disabled="isGroup" size="mini" @click="delTableList">删除</el-button>
                </el-row> -->
                <el-pagination :disabled="!hasTableData" @size-change="listPageChange" @current-change="listPageChange" :current-page.sync="tableFormatData.userTable.pagination.page" :page-sizes="tableFormatData.userTable.pagination.pagesizes" :page-size.sync="tableFormatData.userTable.pagination.list_rows" layout="total, sizes, prev, pager, next, jumper" :total="hasTableData?tableFormatData.userTable.pagination.total : 0">
                </el-pagination>
            </div>
        </div>
    </div>
</div>
</template>

<script>
import {
  clerkEdit,
  clerkGet,
  clerkIndex,
  clerkDelete,
  selecStore,
  clerkStatus
} from '@/api/merchant'
import waves from '@/directive/waves' // Waves directive
import mixins from '@/mixins/mixins'
export default {
  mixins: [mixins.getters('Table'), mixins.getters('Form')],
  name: 'WeChatPublicMenu',
  components: {

  },
  directives: {
    waves
  },
  created() {
    selecStore({
      bus_id: this.$route.query.bus_id
    }).then((response) => {
      const {
        data: {
          data: msgData = {}
        }
      } = response
      //   this.storeListFind = msgData
      this.selecStoreData = msgData
    })
  },
  data() {
    return {
      passwordCopy: '',
      passwordType: 'password',
      initChooseUser: {
        closable: true,
        initList: []
      },
      dialogTableVisible: false,
      selecUseData: [],
      selecStoreData: [],
      formFormatData: {
        key: 'user',
        model: {
          noCreateEdit: false,
          hasMessage: false,
          closeDialog: false,
          bindDialog: 'dialogTableVisible'
        },
        api: {
          editForm: {
            api: clerkGet,
            params: {
              bus_id: this.$route.query.bus_id
            }
          },
          submitForm: clerkEdit,
          change: {
            status: {
              key: 'status',
              api: clerkStatus
            }
          }
        },
        formData: {
          bus_id: this.$route.query.bus_id,
          store_id: '',
          mid: '',
          nickName: '',
          name: '',
          mobile: '',
          type: 0,
          power: [],
          password: '',
          status: 1
        },
        rules: {
          password: [{
            required: true,
            message: '请输入核销密码',
            trigger: 'blur'
          }]
        }
      },
      tableFormatData: {
        current: 'userTable',
        userTable: {
          key: 'userTable',
          api: {
            getList: clerkIndex,
            delList: clerkDelete
          },
          tableData: [],
          files: {
            name: '',
            ids: [],
            checkall: '0',
            Recycle: '1'
          },
          pagination: {
            page: 1,
            list_rows: 10,
            pagesizes: [10, 20, 50],
            total: 0
          }
        }
      }
    }
  },
  methods: {
    saveDialogSubmit() {
      this.formFormatSubmit()
    },
    saveDialogForm() {
      this.resetDialogFormData({ store_id: '', mid: '', nickName: '', name: '', mobile: '', type: 0, power: [], status: 1, bus_id: this.$route.query.bus_id, password: '' })
    },
    formFormatCallBack(data, type, response) {
      // this.filesSerch()
      if (type === 'submitForm') {
        console.log(response, 'formFormatCallBack')
        const { data: { code, msg } } = response
        const hasId = Object.keys(this.$route.query).includes(this.formFormatData.api['editForm']['redirect'] || 'id')
        const isEdit = this.formFormatData.api['editForm'] && this.formFormatData.api['editForm']['api'] && ((this.$route.query && hasId) || (this.formFormatData.api['editForm']['params'] && this.formFormatData.api['editForm']['params']['always']))
        if (code === 1) {
          this.$message.success(isEdit && hasId ? '编辑成功' : '保存成功')
          this.dialogTableVisible = false
          this.getTableList()
        } else {
          this.$message.error(`${msg}`)
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.wechat-public-menu {
    .logo {
        height: 32px;
        width: 32px;
        border-radius: 32px;
        vertical-align: middle;
    }
}
</style>
